import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/Helper.dart';
import '../../helper/global_config.dart';
import '../../pages/things/things_helper.dart';
import '../../widgets/inputWidget.dart';
import '../../model/things.dart';
import '../../helper/routeHelper.dart';
import '../../helper/uidata.dart';
import '../../model/skddl.dart';
import '../skddl/sdddl_authorty.dart';
import '../../widgets/someCards.dart';
import 'things_info.dart';

class ThingsListPage extends StatefulWidget {
  final bool ifAppbar;

  @override
  ThingsListPage(this.ifAppbar);
  @override
  ThingsListPageState createState() => new ThingsListPageState();
}

class ThingsListPageState extends State<ThingsListPage> {
  List<Things> RList = <Things>[];
  List<Things> RList2 = <Things>[];

  bool isEnd = false;
  ScrollController _scrollController = new ScrollController();
  bool firstLoading = false;
  int canShow = 0;

  late Things X1;
  late String sData;

  final TextEditingController c1 = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getRefrush();
  }

  Future<Null> delete(Things r1) async {
    // await addOne();
    await Things.Delete(r1);
    setState(() {
      RList.remove(r1);
    });
  }

  onDeleted(Things r1) => {
        setState(() {
          RList.remove(r1);
        })
      };

  Future<Null> getRefrush() async {
    var list1 = await Things.GetAllList();
    setState(() {
      RList = list1;
      RList2 = list1;
      c1.text = "";
    });
    // search();
  }

  Widget thingsCard(Things x1) {
    return Card(
        key: Key(x1.ID.toString()),
        margin: const EdgeInsets.only(top: 0.2, bottom: 0.2),
        color: GlobalConfig.cardBackgroundColor,
        shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
        child: ListTile(
          //leading: ImageWidget.StationImage(StationList[index].Name),
          title: Text(x1.TName),
          //trailing: Text(StationList[index].STCD),
          onTap: () {
            RouteHelper.ShowPage(context, ThingsInfoPage(x1));
          },
          onLongPress: () {
            RouteHelper.ThingsAdd(context, x1, getRefrush);
          },
        ));
  }

  void search() {
    var list1 = <Things>[];
    sData = c1.text;
    for (var item in RList) {
      if (item.TName.contains(sData) ||
          item.TType.contains(sData) ||
          item.Storage.contains(sData)) {
        list1.add(item);
      }
    }
    RList2 = list1;
    this.setState(() {});
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list5 = <Widget>[];

    list3.add(InputWidget.getSearchButton(c1, search));

    list1.add(InputWidget.inCard2(list3));
    list1.add(listContainer());
    list1.add(InputWidget.inCard2(list4));

    return list1;
  }

  Widget listContainer() {
    return Expanded(
        child: Container(
            // height: 500.0,
            margin: EdgeInsets.all(6.0),
            child: new RefreshIndicator(
              child: ListView.builder(
                itemCount: RList2.length + 1,
                controller: _scrollController,
                physics: const AlwaysScrollableScrollPhysics(),
                itemBuilder: (BuildContext context, int index) {
                  if (index != RList2.length) {
                    return thingsCard(RList2[index]);
                  } else {
                    if (isEnd == true) {
                      return Container(
                          margin: EdgeInsets.all(12.0),
                          child: Center(
                              child: Text(
                            "没有更多了",
                            style: TextStyle(color: Colors.grey),
                          )));
                    } else {
                      return new Container();
                    }
                  }
                },
              ),
              onRefresh: getRefrush,
            )));
  }

  @override
  Widget build(BuildContext context) {
    if (RList == null) return new Container();
    // TODO: implement build
    return new Scaffold(
        appBar: widget.ifAppbar
            ? AppBar(
                title: Text('选择物资'),
                actions: <Widget>[
                  Helper.AuthPB("物资管理")
                      ? IconButton(
                          icon: Icon(
                            Icons.add,
                            color: Colors.white,
                          ),
                          onPressed: () {
                            RouteHelper.ThingsAdd(context, null, getRefrush);
                          })
                      : Container(),
                ],
              )
            : null,
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.refresh),
          onPressed: () => getRefrush(),
        ),
        body: firstLoading == true
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : Column(children: getWidgetList(context)));
  }
}
